package client // Package client import "xemo.top/docker/docker/client"
import (
	"context"
	"encoding/json"
	"xemo.top/docker/docker/api/types"
)

// ContainerExecAttach 将连接附加到服务器中的exec进程。它返回带有被劫持的连接的types.HijackedConnection和a读取器以获取输出。由被调用方通过调用types.HijackedResponse.close来关闭被劫持的连接。
// ContainerExecAttach attaches a connection to an exec process in the server.It returns a types.HijackedConnection with the hijacked connection and the a reader to get output.It's up to the called to close the hijacked connection by calling types.HijackedResponse.Close.
func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) {
	headers := map[string][]string{"Content-Type": {"application/json"}}
	return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, headers)
}

// ContainerExecCreate 创建新的exec配置以运行exec进程
// ContainerExecCreate creates a new exec configuration to run an exec process
func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
	var response types.IDResponse
	if err := cli.NewVersionError("1.25", "env"); len(config.Env) != 0 && err != nil {
		return response, err
	}
	resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
	defer ensureReaderClosed(resp)
	if err != nil {
		return response, err
	}
	err = json.NewDecoder(resp.body).Decode(&response)
	return response, err
}

// ContainerExecInspect 返回有关docker主机上特定exec进程的信息
// ContainerExecInspect returns information about a specific exec process on the docker host
func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
	var response types.ContainerExecInspect
	resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
	if err != nil {
		return response, err
	}
	err = json.NewDecoder(resp.body).Decode(&response)
	defer ensureReaderClosed(resp)
	return response, err
}

// ContainerExecStart 启动已在docker主机中创建的exec进程
// ContainerExecStart starts an exec process already created in the docker host
func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
	resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
	defer ensureReaderClosed(resp)
	return err
}
